import re


def parse_container_info(container_info):
    # 定义正则表达式模式
    pattern = re.compile(
        r'(?P<CONTAINER_ID>\w+)\s+(?P<IMAGE>[\w\/:\.-]+)\s+"(?P<COMMAND>.*?)"\s+(?P<CREATED>[\w\s]+)\s+(?P<STATUS>\w+)\s+(?P<PORTS>[\w\s\.,\/\-\->:]+)\s+(?P<NAMES>[\w-]+)')

    # 匹配每一行的容器信息
    matches = pattern.finditer(container_info)

    # 存储容器信息的列表
    containers = []

    # 遍历匹配结果
    for match in matches:
        container_info_dict = match.groupdict()
        # 将 PORTS 字段中的端口信息分割成列表
        print(container_info_dict)
        container_info_dict['PORTS'] = [port.strip() for port in container_info_dict['PORTS'].split(',')]
        containers.append(container_info_dict)

    return {"containers": containers}


# 你提供的 Docker 容器信息
docker_container_info = """CONTAINER ID   IMAGE                                     COMMAND                  CREATED        STATUS                 PORTS                                                                     NAMES
51be5158d69d   jc21/nginx-proxy-manager:github-pr-3027   "/init"                  3 days ago     Up 6 hours             0.0.0.0:30080->80/tcp, 0.0.0.0:30081->81/tcp, 0.0.0.0:30443->443/tcp      1Panel-nginx-proxy-manager-5Pps
0bbb07c0884f   requarks/wiki:2.5.300                     "docker-entrypoint.s…"   3 days ago     Up 6 hours             3443/tcp, 127.0.0.1:3033->3000/tcp                                        1Panel-wikijs-TUEq
69852d1af726   wordpress:6.3.1                           "docker-entrypoint.s…"   4 weeks ago    Up 6 hours             0.0.0.0:9090->80/tcp                                                      WordPress
328f5c4f7bb1   ohttps/ohttps-nginx                       "sh /data/docker-ent…"   4 weeks ago    Up 6 hours             80/tcp, 0.0.0.0:8443->443/tcp, :::8443->443/tcp                           ohttps-nginx
e02b843b3adc   halcyonazure/lsky-pro-docker:latest       "/entrypoint.sh apac…"   4 weeks ago    Up 6 hours             80/tcp, 0.0.0.0:40027->8089/tcp                                           lsky
a452370c0fe7   openresty/openresty:1.21.4.2-0-focal      "/usr/local/openrest…"   4 weeks ago    Up 6 hours                                                                                       1Panel-openresty-IAjW
7a0891d0987d   mysql:5.7.43                              "docker-entrypoint.s…"   4 weeks ago    Up 6 hours             33060/tcp, 0.0.0.0:3307->3306/tcp                                         mysql5.7
4e1e31edccbe   minio/minio                               "/usr/bin/docker-ent…"   8 weeks ago    Up 6 hours             9000/tcp, 0.0.0.0:9010-9011->9010-9011/tcp, :::9010-9011->9010-9011/tcp   minio
fae81a45c1c8   redislabs/redismod                        "redis-server --load…"   8 weeks ago    Up 6 hours             0.0.0.0:6379->6379/tcp, :::6379->6379/tcp                                 docker-redis-mod-1
833c804e1cb1   yapipro/yapi:1.9.5                        "node server/app.js"     2 months ago   Up 6 hours             0.0.0.0:3000->3000/tcp, :::3000->3000/tcp                                 yapi
7643f3de61b1   mongo:4.2.21                              "docker-entrypoint.s…"   2 months ago   Up 6 hours             0.0.0.0:2717->27017/tcp, :::2717->27017/tcp                               mongodb
b108e0f479b2   xhofe/alist:latest                        "/entrypoint.sh"         2 months ago   Up 6 hours             0.0.0.0:5244->5244/tcp, :::5244->5244/tcp, 5245/tcp                       alist
044502b15c54   whyour/qinglong:latest                    "./docker/docker-ent…"   3 months ago   Up 6 hours (healthy)   0.0.0.0:5700->5700/tcp, :::5700->5700/tcp                                 qinglong
"""

#
# for d in data:
#     print(d)


#

#
title = 'CONTAINER ID   IMAGE                                     COMMAND                  CREATED        STATUS                 PORTS                                                                     NAMES'
# data = '51be5158d69d   jc21/nginx-proxy-manager:github-pr-3027   "/init"                  3 days ago     Up 6 hours             0.0.0.0:30080->80/tcp, 0.0.0.0:30081->81/tcp, 0.0.0.0:30443->443/tcp      1Panel-nginx-proxy-manager-5Pps'

# 计算 CONTAINER ID 和 IMAGE 之间的距离
a = title.find('IMAGE') - title.find('CONTAINER ID')

# 计算 IMAGE 和 COMMAND 之间的距离
b = title.find('COMMAND') - title.find('IMAGE')

# 计算 COMMAND 和 CREATED 之间的距离
c = title.find('CREATED') - title.find('COMMAND')

# 计算 CREATED 和 STATUS 之间的距离
d = title.find('STATUS') - title.find('CREATED')

# 计算 STATUS 和 PORTS 之间的距离
e = title.find('PORTS') - title.find('STATUS')

# 计算 PORTS 和 NAMES 之间的距离
f = title.find('NAMES') - title.find('PORTS')

# 计算NAMES的位置
g = title.find('NAMES')

container_list = docker_container_info.split('\n')
container_dict_data = []
for info in range(0, len(container_list)):
    container_dict = {}
    if info == 0:
        continue
    else:
        container_data = container_list[info]
        container_id = container_data[0:a - 3]
        container_image = container_data[a:a + b - 3]
        container_command = container_data[a + b:a + b + c - 3]
        container_created = container_data[a + b + c:a + b + c + d - 3]
        container_status = container_data[a + b + c + d:a + b + c + d + e - 3]
        container_port = container_data[a + b + c + d + e:a + b + c + d + e + f - 3]
        container_name = container_data[g:]

        container_dict[container_id] = container_id
        container_dict[container_image] = container_image
        container_dict[container_command] = container_command
        container_dict[container_created] = container_created
        container_dict[container_status] = container_status
        container_dict[container_port] = container_port
        container_dict[container_name] = container_name
        container_dict_data.append(container_dict)

print(len(container_dict_data))
